package com.qianya.system.face.web.patient.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianya.model.pojo.patient.qo.PatientOxygenQO;
import com.qianya.model.pojo.patient.qo.PatientOxygenSignQO;
import com.qianya.model.pojo.patient.vo.*;
import com.qianya.system.face.web.patient.model.PatientOxygen;
import com.qianya.system.face.web.patient.service.IPatientOxygenService;
import com.qianya.system.face.web.patient.mapper.PatientOxygenMapper;
import com.qianya.util.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.qianya.model.common.CommonResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qianya.model.common.ResultCode;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
* <p>
*  服务实现类
* </p>
*
* @author lhl
* @since 2020-08-11
*/
@Service
public class PatientOxygenServiceImpl extends ServiceImpl<PatientOxygenMapper, PatientOxygen> implements IPatientOxygenService {
    @Resource
    private PatientOxygenMapper patientOxygenMapper;
        /**
        *查找 
        * @author lhl
        * @since 2020-08-11
        */
        @Override
        public CommonResult find(PatientOxygenQO pOygen) {
            QueryWrapper<PatientOxygen> queryWrapper=new QueryWrapper<PatientOxygen>()
                    .eq("file_id",pOygen.getFileId());
            if (pOygen.getTime()!=null && !"".equals(pOygen.getTime())){
                List<String> list=DateUtils.theSameDay(pOygen.getTime());
                queryWrapper.gt("create_time",list.get(0)).lt("create_time",list.get(1)).orderByDesc("create_time");
            }else {
                queryWrapper.orderByAsc("create_time");
            }
            IPage iPage = page(new Page<PatientOxygen>(pOygen.getPageNo(),pOygen.getPageSize()),queryWrapper);
            return CommonResult.success(iPage);
        }
        /**
        * 新增 
        * @author lhl
        * @since 2020-08-11
        */
        @Override
        public CommonResult create(PatientOxygen patientOxygen) {
            boolean b = save(patientOxygen);
            if (b){
                return CommonResult.success();
            }
            return CommonResult.error(ResultCode.ERROR,"操作失败");
        }


    /**
        * 修改
        * @author lhl
        * @since 2020-08-11
        */
        @Override
        public CommonResult renewal(PatientOxygen patientOxygen) {
            boolean b = updateById(patientOxygen);
            if (b){
                return CommonResult.success();
            }
            return CommonResult.error(ResultCode.ERROR,"操作失败");
        }

    /**
     * 根据日期查询所有患者体征状态
     * @author lhl
     * @since 2020-08-11
     */
    @Override
    public CommonResult patientSign(PatientOxygenSignQO patientOxygenSignQO) {
        if (patientOxygenSignQO.getPageNo()==1 ||patientOxygenSignQO.getPageNo()==0){
            patientOxygenSignQO.setPageNo(0);
        }else {
            patientOxygenSignQO.setPageNo(patientOxygenSignQO.getPageSize()*(patientOxygenSignQO.getPageNo()-1));
        }
        boolean flag=false;
        if ("".equals(patientOxygenSignQO.getTime())){
            flag=true;
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            patientOxygenSignQO.setTime(sdf.format(new Date()));
        }
        List<String> list=DateUtils.theSameDay(patientOxygenSignQO.getTime());
        if (flag){
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH");
            list.set(1,sdf.format(new Date()));
        }
        List<PatientFileInfoSignVO> pfirList=patientOxygenMapper.patientSign(list.get(0),list.get(1),patientOxygenSignQO);
        Integer count=patientOxygenMapper.patientSignCount(list.get(0),list.get(1),patientOxygenSignQO);
        for (int i = 0; i <pfirList.size() ; i++) {  //95-97  //60-100
            pfirList.get(i).setFileTime(patientOxygenSignQO.getTime());
            String[] intBo=pfirList.get(i).getBloodOxygen().split(",");
            String[] intHr=pfirList.get(i).getBloodOxygen().split(",");
            StringBuffer sb=new StringBuffer("");
            for (int j = 0; j < intBo.length; j++) { //血氧.
                int bo=Integer.valueOf(intBo[j]);
                if (95>bo){
                    sb.append("血氧浓度过低 ");
                    break;
                }else if (97<bo){
                    sb.append("血氧浓度过高 ");
                    break;
                }
            }
            for (int j = 0; j < intHr.length; j++) { //心率
                int hr=Integer.valueOf(intHr[j]);
                if (60>hr){
                    sb.append("心率过低 ");
                    break;
                }else if (100<hr){
                    sb.append("心率过高 ");
                    break;
                }
            }
            if ("".equals(sb.toString())){
                pfirList.get(i).setBrState("正常");
            }else {
                pfirList.get(i).setBrState(sb.toString());
            }


        }
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("recode",pfirList);
        map.put("totol",count);
        map.put("pageNo",patientOxygenSignQO.getPageNo());
        map.put("pageSize",patientOxygenSignQO.getPageSize());
        return CommonResult.success(map);
    }

    @Override
    public List<MonthVo> eatStatistics() {

        Calendar end = Calendar.getInstance();

        end.setTime(new Date());
        end.add(Calendar.DAY_OF_MONTH, 1);

        Calendar begin = Calendar.getInstance();

        BeanUtils.copyProperties(end,begin);

        begin.add(Calendar.YEAR, -1);

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-01");

        List<MonthVo> patientStatic=patientOxygenMapper.eatStatistics(sdf.format(begin.getTime()),sdf.format(end.getTime()));
        return DateUtils.listChangeMapReturn(patientStatic,sdf.format(begin.getTime()), sdf.format(end.getTime()));

    }

    /**
     * 服药异常报告
     * @return
     */
    @Override
    public int findsignsreport() {


     int count =patientOxygenMapper.findsignsreport();

     return  count;
    }


    /**
     * @desc 时间转换
     * @author lhl
     * @date 2020-07-24 15:43
     * @return
     */
    public Date timeTrans(Date time){
        SimpleDateFormat sdf=new SimpleDateFormat("HH");
        Calendar cal=Calendar.getInstance();
        cal.setTime(time);
        Integer hours=Integer.valueOf(sdf.format(time));
        if (hours<8){
            cal.set(Calendar.HOUR_OF_DAY, -6);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
        }else if (hours<12){
            cal.set(Calendar.HOUR_OF_DAY, 8);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
        }else if (hours<18){
            cal.set(Calendar.HOUR_OF_DAY, 12);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
        }else {
            cal.set(Calendar.HOUR_OF_DAY, 18);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
        }

        return cal.getTime();
    }
}
